﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using MyWeb.Data;
using MyWeb.Business;
using MyWeb.Common;
using System.Security.Cryptography;

namespace MyWeb.Modules.Page
{
    public partial class Logon : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnLogon_Click(object sender, EventArgs e)
        {
            MD5 md5hash=MD5.Create();
            string UId = txtUsername.Text.Trim();
            string PId = txtPassword.Text.Trim();
            List<Data.Users> list = new List<Data.Users>();
            list = UsersService.Users_GetByAll();
            foreach(Data.Users tv in list)
            {
            if (tv.User.Equals(txtUsername.Text.Trim()) && MaHoaPass.VerifyMd5Hash(md5hash, txtPassword.Text.Trim(), tv.Pass.Trim()))
            {               
                FormsAuthentication.SetAuthCookie(UId, false);
                Session["uID"] = tv.uID;
                Session["Admin"] = tv.Admin;
                Session["Level"] = tv.Level;
                Response.Redirect("/Admin/Default.aspx");
            }
            }
                txtPassword.Text = "";
                txtPassword.Focus();
                ltrError.Text = "Đăng nhập không thành công!";
        
        }
    }
}